﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;

namespace Sinacor.OMM.Deployment.DataAccess
{
    public class OracleDataBase : IDisposable
    {
        private Dictionary<string, System.Data.OracleClient.OracleConnection> _connections;
        private const string SYSTEM_DATABASE_KEY = "SystemDatabase";
        private string _dataSource;
        private bool _isCompanyPasswordEncrypted;

        #region Constructor
        
        public OracleDataBase(string userName, string password, string dataSource, bool isCompanyPasswordEncrypted)
        {
            _isCompanyPasswordEncrypted = isCompanyPasswordEncrypted;
            _dataSource = dataSource;
            _connections = new Dictionary<string, OracleConnection>();

            //criar a conexao com a base de sistema
            OracleConnection systemDatabaseConnection = new OracleConnection(CreateConnectionString(userName, password, dataSource));
            _connections.Add(SYSTEM_DATABASE_KEY, systemDatabaseConnection);
        }

        #endregion


        protected string CreateConnectionString(string user, string password, string datasource)
        {
            if (user == null)
                throw new ArgumentNullException("user");
            if (password == null)
                throw new ArgumentNullException("password");
            if (datasource == null)
                throw new ArgumentNullException("datasource");
            
            string maxPoolSize = "5";
            string minPoolSize = "1";
            string pooling = "true";

            string newConnection = "Pooling=" + pooling +
                                   ";Min Pool Size=" + minPoolSize +
                                   ";Max Pool Size=" + maxPoolSize +
                                   ";Password=" + password +
                                   ";User ID=" + user +
                                   ";Data Source=" + datasource +
                                   ";Persist Security Info=True";
            return newConnection;
        }
        
        public bool ExecuteNonQuery(string query)
        {
            OracleConnection connection = this.GetSystemConnection();
            return ExecuteNonQuery(query, connection);
        }

        public IDataReader ExecuteReader(string query)
        {
            OracleConnection connection = this.GetSystemConnection();
            return ExecuteReader(query, connection);
        }

        protected OracleConnection GetSystemConnection()
        {
            if (_connections.ContainsKey(SYSTEM_DATABASE_KEY))
                return _connections[SYSTEM_DATABASE_KEY];
            else
                return null;
        }

        private bool ExecuteNonQuery(string query, OracleConnection connection)
        {
            try
            {
                connection.Open();
                using (OracleCommand command = new OracleCommand(query, connection))
                {
                    return command.ExecuteNonQuery() > 0;
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                connection.Close();
            }
        }

        private IDataReader ExecuteReader(string query, OracleConnection connection)
        {            
            if (connection.State != ConnectionState.Open)
                connection.Open();

            using (OracleCommand command = new OracleCommand(query, connection))
            {
                return command.ExecuteReader();
            }            
        }        

        #region IDisposable Members

        public void Dispose()
        {
            foreach (OracleConnection connection in this._connections.Values)
            {
                connection.Close();
                connection.Dispose();
            }
            this._connections.Clear();
        }

        #endregion
    }
}